/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/*****************************************************************************
* @brief	include
*****************************************************************************/
#define _MAPP_MODULE_
#include "mbapp.h"
#include "os_obj.h"       
#include "sys_task.h"
#include "common.h"

/*****************************************************************************
* @brief	TEST MODE SELECT
* OPTION:	MB_RTU_SLAVE , MB_RTU_MASTER , ASCII_SLAVE, ASCII_MASTER
*****************************************************************************/
#define MB1_MODE		MB_RTU_SLAVE
//#define MB3_MODE		MB_RTU_SLAVE

/*****************************************************************************
* @brief	local var
*****************************************************************************/
mb_baud_tab_create;
    
uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

typedef struct
{
    uint16_t reg_r[64];
    uint16_t reg_w[64];
    uint16_t pv_r[64];
    uint16_t pv_w[64];
    uint16_t obj_size;
} mb_tst_type;

mb_tst_type mb_tst;

/*****************************************************************************
* @brief	local fun
*****************************************************************************/
void rcv_end_handle_comp(uint8_t ErrSt);

/*****************************************************************************
* @brief	modbus task.
* @param	argument
* @return	none
*****************************************************************************/   
void modbus_task (void *argument)
{
    UINT os_err;
    ULONG event;
    mb_cmd_buff_type mcmd;
		
    mb_obj_init(&mb.obj01);
	mb_obj_init(&mb.obj03);
	
	// mb1 config
	#ifdef MB1_MODE
    mb01_Init(MB1_MODE, mb_baud_tab[MB_BAUD_19200], MB_PAR_NONE);
    mb.obj01.slave_id = 1;
	mb.obj01.rtu_len_cut = 1;
    mb.obj01.rtu_master_delay_set = 3;
    mb.obj01.rcv_end_handle_comp = rcv_end_handle_comp;
    mb.obj01.os_event_send = mb_os_send;
    mb.obj01.os_event_base = 1<<4;
	
    //cmd config
	mcmd.device_id = 0;
	mcmd.cmd = FUN_CODE_WRITE_MULTIPLE_REG;
	mcmd.dat = mb_tst.pv_w;
	mcmd.dat_addr = 0;
	mcmd.amount = 48;
	mcmd.call_back=0;
    mb.obj01.api->stc_cmd_req(0, &mcmd);
	
	mcmd.cmd = FUN_CODE_READ_REG;
	mcmd.dat = mb_tst.pv_r;
	mb.obj01.api->stc_cmd_req(1, &mcmd);	
    #endif
	
    // mb2 config
	#ifdef MB3_MODE
    mb03_Init(MB3_MODE, mb_baud_tab[MB_BAUD_19200], MB_PAR_NONE);
    mb.obj03.slave_id = 1;
    mb.obj03.rtu_rcv_end_delay_set = 3;
    mb.obj03.rcv_end_handle_comp = rcv_end_handle_comp;
    mb.obj03.os_event_send = mb_os_send;
    mb.obj03.os_rtu_end_event = 4;
    mb.obj03.os_timeout_event = 8;
	
    //cmd config
	mcmd.device_id = 0;
	mcmd.cmd = FUN_CODE_WRITE_MULTIPLE_REG;
	mcmd.dat = mb_tst.pv_w;
	mcmd.dat_addr = 0;
	mcmd.amount = 48;
	mcmd.call_back=0;
    mb.obj03.api->stc_cmd_req(0, &mcmd);
	
	mcmd.cmd = FUN_CODE_READ_REG;
	mcmd.dat = mb_tst.pv_r;
	mb.obj03.api->stc_cmd_req(1, &mcmd);	
    #endif
	
    //task handle
    for(;;)
    {
        // wait modbus event
        os_err = tx_event_flags_get(os_obj.eid_mb, 
                           0xFFFF,
                           TX_OR_CLEAR, 
                           &event,
                           ms_ticks(3000));
        // os_api_exe_check();
        
        (void)os_err;

		#ifdef MB1_MODE
        mb_poll(&mb.obj01);
        #endif
		
		#ifdef MB3_MODE
        mb_poll(&mb.obj03);
        #endif
		
        mb_tst.obj_size = sizeof(mb.obj03);
    }
}

/*****************************************************************************
* @brief	callback.
* @param	ErrSt
* @return	none
*****************************************************************************/
void rcv_end_handle_comp(uint8_t ErrSt)
{

}

